﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace GoodMan.Infrastructure.Entities.Repository
{
    [Table("T_OrderDetail")]
    public class T_OrderDetail :IEntity<T_OrderDetail>
    {
        [Key]
        [Column(TypeName="int")]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        public int OrderId { get; set; }
        public int AreaCode { get; set; }
        public int ProductId { get; set; }
        [Column(TypeName="nvarchar")]
        [StringLength(128)]
        public string ProductName { get; set; }
        public int ProductType { get; set; }
        public int ProductSubType { get; set; }
        public int Num { get; set; }
        [Column(TypeName = "decimal")]
        public decimal Price { get; set; }
        public int PolicyId { get; set; }

        [Column(TypeName = "decimal")]
        public decimal OffPrice { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(512)]
        public string Remark { get; set; }
        /// <summary>
        /// 配菜信息
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(1024)]
        public string Catering { get; set; }

        public override T_OrderDetail Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }

        public override T_OrderDetail Find(System.Linq.Expressions.Expression<Func<T_OrderDetail, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.OrderDetail.FirstOrDefault(where);
                if (item != null)
                {
                    this.Id = item.Id;
                    this.OrderId = item.OrderId;
                    this.AreaCode = item.AreaCode;
                    this.Num = item.Num;
                    this.OffPrice = item.OffPrice;
                    this.PolicyId = item.PolicyId;
                    this.Price = item.Price;
                    this.ProductId = item.ProductId;
                    this.ProductName = item.ProductName;
                    this.ProductSubType = item.ProductSubType;
                    this.ProductType = item.ProductType;
                    this.Remark = item.Remark;
                    this.Catering = item.Catering;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_OrderDetail, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.OrderDetail.Any(where);
            }
        }

        public override T_OrderDetail FindLast(System.Linq.Expressions.Expression<Func<T_OrderDetail, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.OrderDetail.OrderByDescending(c=>c.Id).FirstOrDefault(where);
                if (item != null)
                {
                    this.Id = item.Id;
                    this.OrderId = item.OrderId;
                    this.AreaCode = item.AreaCode;
                    this.Num = item.Num;
                    this.OffPrice = item.OffPrice;
                    this.PolicyId = item.PolicyId;
                    this.Price = item.Price;
                    this.ProductId = item.ProductId;
                    this.ProductName = item.ProductName;
                    this.ProductSubType = item.ProductSubType;
                    this.ProductType = item.ProductType;
                    this.Remark = item.Remark;
                    this.Catering = item.Catering;
                    return this;
                }
                return null;
            }
        }
    }
}
